import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || '',
    userInfo: (() => {
      try {
        return info ? JSON.parse(info) : null
      } catch (e) {
        return null
      }
    })()
  },
  mutations: {
    setUserInfo(state, data) {
      state.token = data.token
      localStorage.setItem('token', data.token)
    },
    clearUserInfo(state) {
      state.token = ''
      localStorage.removeItem('token')
    }
  },
  getters: {
    isLoggedIn: state => !!state.token,
  }
})